using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;


namespace con0
{
    class Program
    {
        static List<string> toList = new List<string>();
        static List<string> createList = new List<string>();
        static List<string> errorList = new List<string>();

        static void Main(string[] args)
        {
            if (args.GetLength(0) != 2 && args.GetLength(0) != 3)
            {
                Console.WriteLine("wrong usage!");
            }

            else if (args.GetLength(0) == 2)
            {
                update(fillListFromFile(args[1], true), fillListFromFile(args[0], false));
            }


            else if (args.GetLength(0) == 3)
            {
                if (args[2].ToLower() == "/e" || args[2].ToLower() == "-e")
                    updateWithEnum(fillListFromFile(args[1], true), fillListFromFile(args[0], false));
            }
            printError();
        }



        private static void updateWithEnum(List<string> inDirs, List<string> outDirs)
        {
            foreach (string create in outDirs)
            {
                foreach (string to in inDirs)
                {
                    foreach (string dir in Directory.GetDirectories(create + getFirstDirs(to)))
                    {
                        try
                        {
                            Console.WriteLine("Updating: " + create + to);
                            if (getEnum(getLastDir(dir)) == getEnum(getLastDir(to)))
                                errorList.Add("The folder '" + dir + "' exists and has the same Enumerator '" + getEnum(getLastDir(dir)) + "' like '" + getLastDir(to) + "'.");
                            else
                                Directory.CreateDirectory(create + to);
                        }
                        catch (Exception ex)
                        {
                            errorList.Add(ex.Message);
                        }
                    }
                }
            }
        }

        private static void update(List<string> inDirs, List<string> outDirs)
        {
            foreach (string create in outDirs)
            {
                foreach (string to in inDirs)
                {
                    try
                    {
                        Console.WriteLine("Updating: " + create + to);
                        if (!System.IO.Directory.Exists(create + to))
                            System.IO.Directory.CreateDirectory(create + to);

                    }
                    catch (Exception ex)
                    {
                        errorList.Add(ex.Message);
                    }
                }
            }
        }


        private static List<string> fillListFromFile(string path, bool removeLastBackslash)
        {
            List<string> tmpList = new List<string>();

            foreach (string line in File.ReadAllLines(path))
            {
                if (removeLastBackslash)
                {
                    if (line.EndsWith("\\"))
                        tmpList.Add(line.Substring(0, line.Length - 1));
                    else
                        tmpList.Add(line);
                }
                else
                {
                    if (!line.EndsWith("\\"))
                        tmpList.Add(line + "\\");
                    else
                        tmpList.Add(line);
                }
            }
            return tmpList;
        }

        private static void printError()
        {
            Console.WriteLine("\n\nErrors:\n");
            if (errorList.Count == 0) { Console.WriteLine("\t- No Error Ocurred"); }
            else
            {
                foreach (string error in errorList)
                    Console.WriteLine(error);
                Console.WriteLine("\n\tTotal Errors ocurred: " + errorList.Count);
            }
        }

        private static bool hasEnum(string check)
        {
            Regex reg = new Regex("(\\d+)(_)(.*)", RegexOptions.IgnoreCase | RegexOptions.Singleline);
            return reg.Match(check).Success;
        }

        private static int getEnum(string pathWithEnum)
        {
            Regex reg = new Regex("(\\d+)(_)(.*)");
            Match m = reg.Match(pathWithEnum);
            if (m.Success)
                return Convert.ToInt32(m.Groups[1].ToString());
            else
                return -1;
        }

        private static string getLastDir(string path)
        {
            if (path.Contains("\\"))
                return path.Substring(path.LastIndexOf("\\") + 1);
            else
                return path;
        }

        private static string getFirstDirs(string path)
        {
            if (!path.Contains("\\"))
                return null;
            else
                return path.Substring(0, path.LastIndexOf("\\"));
        }
    }
}
